<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/08/01
 * Time: 16:14
 */

class CouponPush extends Eloquent
{
    protected $table = "coupon_push";

    public static function Coupon($user_id){
        return CouponPush::where('user_id',$user_id)->where('status',1)->first();
    }

    //查询有效的推送信息
    public static function validCoupon($user_id, $coupon_id, $coupon_base_info){
        return CouponPush::where('user_id',$user_id)->where('coupon_id',$coupon_id)->where('coupon_base_info_id',$coupon_base_info)->where('status',1)->first();
    }

    public static function Coupons($coupon_id, $coupon_base_info){
        return CouponPush::where('coupon_id',$coupon_id)->where('coupon_base_info_id',$coupon_base_info)->where('status',1)->first();
    }

    //判断推送时间是否超过三分钟
    public static function ThreePush($id){
        $res = CouponPush::find($id);
        $time = 0;
        if(!empty($res)){
            $push_time = date("Y-m-d H:i:s", strtotime("$res->updated_at +3 min"));
            $now_time = date("Y-m-d H:i:s",time());
            if($push_time > $now_time){
                $time = 1;
            }
        }
        return $time;
    }

    //获取当前时间
    public static function now_time(){
       return $now_time = date("Y-m-d H:i:s",time());
    }

    public static function date_time(){
        return $now_time = date("Y-m-d",time());
    }

    //判断延顺时间
    public static function Limit_time($time,$limit_days){
        $time = 0;
        if(date("H:i", strtotime("$time +$limit_days day") > self::now_time())){
            return $thme = 1;   //有效
        }
        return $time;
    }

    //取消推送
    public static function cancelPushCoupon($users_id, $coupon_id, $coupon_base_info_id){
        $response = array('code' => 0, 'msg' => 'ok');
        //判断推送列表是否有有效的数据
        $pushList = CouponPush::validCoupon($users_id, $coupon_id, $coupon_base_info_id);
        if(empty($pushList)){
            $response = Lang::get("error.coupon_not_push_error_71015");
        }
        $pushList->status = 2;
        $pushList->updated_at = CouponPush::now_time();
        if(!$pushList->save()){
            $response = Lang::get("error.coupon_cancel_push_error_71014");
        }
        Log::error("error", array("system" => "api3.0" ,"event" => "cancelPushCoupon", 'input' =>Input::all() ,"exception" => $response));
        return $response;
    }

    //根据手机号获取用来规划推送的优惠券、
    public static function getCoupon($phone){
        return CouponPush::where('phone',$phone)->where('status',1)->first();
    }

    public static function delPush($pushList){
        try{
            if(isset($pushList)){
                $push_couopn = CouponPush::Coupons($pushList->coupon_id,$pushList->coupon_base_info_id);
                if(!is_null($push_couopn)){
                    $push_couopn->status = 2;
                    $push_couopn->updated_at = Common::currentTimeString();
                    $push_couopn->save();
                }
            }
        }catch (Exception $e){
            Log::error("error", array("system" => "api3.0" ,"event" => "verification_coupons", 'input' =>Input::all() ,"exception" => $e));
        }
    }
}